package com.liguang.rcs.admin.db.converter;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Lists;
import com.liguang.rcs.admin.util.JacksonUtil;

import javax.persistence.AttributeConverter;
import java.util.List;

public class JpaAttributeConverter {
    /**
     * Long 数组  string 互转
     */
    public static class LongArray2Str implements AttributeConverter<Long[], String> {

        @Override
        public String convertToDatabaseColumn(Long[] attribute) {
            return JacksonUtil.toJson(Lists.newArrayList(attribute));
        }

        @Override
        public Long[] convertToEntityAttribute(String dbData) {
            List<Long> longs = JacksonUtil.toObject(dbData, new TypeReference<List<Long>>() {
            });
            if (longs == null) {
                return null;
            }
            return longs.toArray(new Long[longs.size()]);
        }
    }
}
